export module GroupLocation;

import DND;

import DefOrder;
import DefVar;
import ResMgr;
import Facility;
import Database;
import FacilityLocation;

//定居点界面
export class GroupLocation
{
public:
	DND_CLASS_TYPE("location", 0);

	void Init(UI::Panel* panel)
	{
		_grp = panel->CreateGroup({ DefVar::UI_SX_RIGHT, 200 }, { 1.0, 0.25 },
			Locator{ {}, {0, DefVar::UI_DY_0}, Locator::Layout::COLUMN });
		_grp->SetAutoRun(false);

		{
			size_t size = g_lang->GetTagSize("ui_location");
			for (size_t i = 0; i < size; ++i)
			{
				auto* txt = UI::Text::Create(g_res->CreateTextNormal());
				txt->SetLang("ui_location", std::to_string(i));
				_grp->Add(&txt->_cpts);
			}
		}

		_grp->SetBackgroud("bg", { DefVar::UI_W, DefVar::UI_H }, DefVar::UI_OFFSET_BG);

		//right
		_grpRight = panel->CreateGroup({ DefVar::UI_SX_RIGHT + DefVar::UI_DX_0, 200 }, DefVar::UI_RIGHT_0,
			Locator{ {}, {0, DefVar::UI_DY_0}, Locator::Layout::COLUMN });
		_grpRight->SetAutoRun(false);

		//0名称输入框
		{
			auto* edt = g_res->CreateEditBox();
			_grpRight->Add(&edt->_cpts);

			_edtName = edt;
		}

		//级别
		_grpType = panel->CreateGroup(
			{ DefVar::UI_SX_RIGHT + DefVar::UI_DX_0, 200 + DefVar::UI_DY_0 },
			DefVar::UI_RIGHT_0,
			Locator{ {}, {DefVar::UI_DX_BUTTON_TEXT, 0}, Locator::Layout::ROW });
		_grpType->SetAutoRun(false);
		{
			size_t size = g_lang->GetTagSize("location_type");
			for (size_t i = 0; i < size; ++i)
			{
				auto* btn = g_res->CreateButtonText();
				btn->SetMode(UI::Control::Mode::RADIO);
				btn->SetLang("location_type", std::to_string(i));
				_grpType->Add(&btn->_cpts);
			}
		}

	}

	void Refresh(FacilityLocation& data, bool to_ui)
	{
		if (to_ui)
		{//输入框不是焦点才写入
			if(!_edtName->GetEditBox()->IsFocus())
				_edtName->GetEditBox()->SetString(data._name);
			_grpType->SetIndexOpen(data._type);
		}
		else
		{//输入框丢失焦点，更新数据
			if (_edtName->GetEditBox()->IsLoseFocus())
				data._name = _edtName->GetEditBox()->GetString();
			data._type = _grpType->GetIndexOpen();
			data.RefreshUI();
		}
		
	}

	void Run(LogicType lt)
	{
		_grp->Run(lt);
		_grpRight->Run(lt);
		_grpType->Run(lt);
	}
private:
	UI::Group* _grp;
	UI::Group* _grpRight;//主列
	UI::Group* _grpType;//级别

	UI::EditBoxNormal* _edtName;
};